home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6609 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  81 lines

  1. Newsgroups: comp.lang.c
  2. Path: uu4news.netcom.com!zodiac!szh
  3. From: szh@zcon.com (Syed Zaeem Hosain)
  4. Subject: Re: Ability to locate spaces?
  5. Message-ID: <1996Feb16.162842.18380@zcon.com>
  6. Sender: szh@zcon.com (Syed Zaeem Hosain)
  7. Nntp-Posting-Host: zodiac
  8. Reply-To: szh@zcon.com
  9. Organization: Z Consulting Group
  10. References: <Pine.SOL.3.91.960215222301.15979A-100000@teer1.acpub.duke.edu>
  11. Date: Fri, 16 Feb 1996 16:28:42 GMT
  12.  
  13. In article <Pine.SOL.3.91.960215222301.15979A-100000@teer1.acpub.duke.edu>, John Young Oh <jyo@acpub.duke.edu> writes:
  14. >I am trying to write a program that reads an input file and locates where
  15. >the spaces are located. For example, if a line from the input file read:
  16. >555555.55    5555.55   5555.55
  17. >
  18. >I would like the program to be able to recognize that there are spaces
  19. >between the numbers and keep track of them. What I did was to use
  20. >fgets and read in the line into a string. I then used a for loop
  21. >and checked each array element of the character string and used an
  22. >if statement to see if an array element was a space.
  23. >Here are the lines of code:
  24. >
  25. >FILE *input;
  26. >char *string, buf[200];
  27. >int i, count;
  28. >input = fopen ("data", "r");
  29. >(etc etc etc)
  30. >string = fgets (buf, 199, input);
  31. >for (i = 0; i < 40; ++i)
  32. >{
  33. >    if (buf[i] == ' ')
  34. >    {
  35. >        ++count;
  36. >    }
  37. >}
  38. >
  39. >What is wrong with this code? It does not seem to work.
  40.  
  41. Well, what did the program do? What is the error? Is it a compiler
  42. problem or a run-time problems? "Does not seem to work" is pretty darn
  43. vague, in other words!  :-)
  44.  
  45. Here is what I would check:
  46.  
  47. 1. Did you initialize count to 0 before the lines get read?
  48.  
  49. 2. Did you check to see if fgets returns a legit result? I.e.  the line
  50. has something to read?
  51.  
  52. 3. Is the line longer than 40 characters? If so, why does your loop
  53. stop at 40 characters? Why not use the `\n` byte as a terminator for
  54. the loop? Or the '\0' byte for that matter?
  55.  
  56. 4. Are the lines longer than 199 characters? If so, why is your "buf"
  57. size set to 200?
  58.  
  59. 5. Did the call to "fopen" succeed? Are you reading real lines from
  60. the input program?
  61.  
  62. 6. Is the filename correct? Does the file called "data" exist?
  63.  
  64. There may be other issues, but these are the ones that come to mind off
  65. the top of my head since I do not know the symptoms you are actually
  66. seeing when running the program.
  67.  
  68. As an aside: your first sentence says "... locates where the spaces are
  69. located".  Later you say "recognize that there are spaces ...  and keep
  70. track of them". What are you really trying to do - simply count them or
  71. something else?
  72.  
  73.                                 Z
  74.  
  75.  
  76. -- 
  77. -------------------------------------------------------------------------
  78. | Syed Zaeem Hosain          P. O. Box 610097            (408) 441-7021 |
  79. | Z Consulting Group        San Jose, CA 95161             szh@zcon.com |
  80. -------------------------------------------------------------------------
  81.